package com.umeng.message.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.text.TextUtils;

import com.bytedance.common.utility.Logger;

public class MessageProvider extends ContentProvider {
    class a extends SQLiteOpenHelper {
        public a(MessageProvider arg4, Context arg5) {
            super(arg5, "MessageStore.db", null, 2);
        }

        public void onCreate(SQLiteDatabase arg3) {
            Logger.d(MessageProvider.a, "MessageStoreHelper-->onCreate-->start");
            arg3.execSQL("CREATE TABLE IF NOT EXISTS MessageStore(_id Integer  PRIMARY KEY  AUTOINCREMENT  , MsdId Varchar  , Json Varchar  , SdkVersion Varchar  , ArrivalTime Long  , ActionType Integer )");
            arg3.execSQL("create table if not exists MsgTemp(tempkey varchar default NULL, tempvalue varchar default NULL,PRIMARY KEY(tempkey))");
            arg3.execSQL("create table if not exists MsgAlias(time long,type varchar default NULL,alias varchar default NULL,exclusive int,error int,message varchar,PRIMARY KEY(time))");
            Logger.d(MessageProvider.a, "MessageStoreHelper-->onCreate-->end");
        }

        public void onUpgrade(SQLiteDatabase arg3, int arg4, int arg5) {
            this.onCreate(arg3);
            Logger.d(MessageProvider.a, "MessageStoreHelper-->onUpgrade");
        }
    }

    class b extends SQLiteOpenHelper {
        public b(MessageProvider arg4, Context arg5) {
            super(arg5, "MsgLogStore.db", null, 5);
        }

        boolean a(SQLiteDatabase arg4, String arg5) {
            boolean v0 = false;
            if (!TextUtils.isEmpty(((CharSequence) arg5))) {
                try {
                    Cursor v1_1 = arg4.rawQuery("select count(*) as c from sqlite_master where type = \'table\' and name = \'" + arg5.trim() + "\'", null);
                    if ((v1_1.moveToNext()) && v1_1.getInt(0) > 0) {
                        v0 = true;
                    }

                    if (v1_1 == null) {
                        return v0;
                    }

                    v1_1.close();
                } catch (Throwable v1) {
                }
            }

            return v0;
        }

        public void onCreate(SQLiteDatabase arg3) {
            arg3.execSQL("create table if not exists MsgLogStore (MsgId varchar, ActionType Integer, Time long, PRIMARY KEY(MsgId, ActionType))");
            arg3.execSQL("create table if not exists MsgLogIdTypeStore (MsgId varchar, MsgType varchar, PRIMARY KEY(MsgId))");
            arg3.execSQL("create table if not exists MsgLogStoreForAgoo (MsgId varchar, TaskId varchar, MsgStatus varchar, Time long, PRIMARY KEY(MsgId, MsgStatus))");
            arg3.execSQL("create table if not exists MsgLogIdTypeStoreForAgoo (MsgId varchar, TaskId varchar, MsgStatus varchar, PRIMARY KEY(MsgId))");
            arg3.execSQL("create table if not exists MsgConfigInfo (SerialNo integer default 1, AppLaunchAt long default 0, UpdateResponse varchar default NULL)");
            Logger.d(MessageProvider.a, "MsgLogStoreHelper-->onCreate");
        }

        public void onUpgrade(SQLiteDatabase arg4, int arg5, int arg6) {
            Logger.d(MessageProvider.a, "oldVersion:" + arg5 + ",newVersion:" + arg6);
            if (arg5 <= 4) {
                Logger.d(MessageProvider.a, "MsgLogStoreHelper-->drop delete");
                arg4.execSQL("drop table MsgConfigInfo");
            }

            this.onCreate(arg4);
            Logger.d(MessageProvider.a, "MsgLogStoreHelper-->onUpgrade");
        }
    }

    static final String a = MessageProvider.class.getSimpleName();
    static final UriMatcher b = new UriMatcher(-1);
    a c;
    SQLiteDatabase d;
    b e;
    SQLiteDatabase f;
    static final int g = 1;
    static final int h = 2;
    static final int i = 3;
    static final int j = 4;
    static final int k = 5;
    static final int l = 6;
    static final int m = 7;
    static final int n = 8;
    static final int o = 9;
    static Context p;

    public MessageProvider() {

    }

    private void b() {
        synchronized (this) {
            this.c = new a(this, this.getContext());
            this.e = new b(this, this.getContext());
            if (this.d == null) {
                this.d = this.c.getWritableDatabase();
            }

            if (this.f == null) {
                this.f = this.e.getWritableDatabase();
            }
        }
    }

    public int delete(Uri arg7, String arg8, String[] arg9) {
        int v0_1;
        Throwable v1;
        try {
            switch (MessageProvider.b.match(arg7)) {
                case 2:
                    v0_1 = this.d.delete("MsgTemp", arg8, arg9);
                    break;
                case 3:
                    v0_1 = this.d.delete("MsgAlias", arg8, arg9);
                    break;
                case 4:
                    v0_1 = this.d.delete("MsgAlias", null, null);
                    break;
                case 5:
                    v0_1 = this.f.delete("MsgLogStore", arg8, arg9);
                    break;
                case 6:
                    v0_1 = this.f.delete("MsgLogIdTypeStore", arg8, arg9);
                    break;
                case 7:
                    v0_1 = this.f.delete("MsgLogStoreForAgoo", arg8, arg9);
                    break;
                case 8:
                    v0_1 = this.f.delete("MsgLogIdTypeStoreForAgoo", arg8, arg9);
                    break;
                default:
                    v0_1 = 0;
                    break;
            }
        } catch (Throwable v0) {
            Throwable v5 = v0;
            v0_1 = 0;
            v1 = v5;
            v1.printStackTrace();
            return v0_1;
        }

        try {
            this.getContext().getContentResolver().notifyChange(arg7, null);
            return v0_1;
        } catch (Throwable v2) {
            v2.printStackTrace();
            return v0_1;
        }
    }

    public String getType(Uri arg4) {
        switch (MessageProvider.b.match(arg4)) {
            case 1:
            case 2:
            case 3:
            case 5:
            case 7:
            case 8:
            case 9: {
                return "vnd.android.cursor.dir/vnd.umeng.message";
            }
        }

        throw new IllegalArgumentException("Unknown URI " + arg4);
    }

    public Uri insert(Uri arg9, ContentValues arg10) {
        Uri v0_1;
        long v6 = 0;
        Uri v1 = null;
        try {
            long v2;
            switch (MessageProvider.b.match(arg9)) {
                case 1: {
                    v2 = this.d.insertWithOnConflict("MessageStore", null, arg10, 5);
                    if (v2 <= v6) {
                        return v1;
                    }
                    com.umeng.message.provider.a.a(MessageProvider.p);
                    v0_1 = ContentUris.withAppendedId(com.umeng.message.provider.a.b, v2);
                    this.getContext().getContentResolver().notifyChange(v0_1, null);
                    return v0_1;
                }
                case 2: {
                    v2 = this.d.insertWithOnConflict("MsgTemp", null, arg10, 5);
                    if (v2 <= v6) {
                        return v1;
                    }

                    com.umeng.message.provider.a.a(MessageProvider.p);
                    v0_1 = ContentUris.withAppendedId(com.umeng.message.provider.a.b, v2);
                    this.getContext().getContentResolver().notifyChange(v0_1, null);
                    return v0_1;
                }
                case 3: {
                    v2 = this.d.insertWithOnConflict("MsgAlias", null, arg10, 5);
                    if (v2 <= v6) {
                        return v1;
                    }
                    com.umeng.message.provider.a.a(MessageProvider.p);
                    v0_1 = ContentUris.withAppendedId(com.umeng.message.provider.a.d, v2);
                    this.getContext().getContentResolver().notifyChange(v0_1, null);
                    return v0_1;
                }
                case 5: {
                    v2 = this.f.insertWithOnConflict("MsgLogStore", null, arg10, 5);
                    if (v2 <= v6) {
                        return v1;
                    }

                    com.umeng.message.provider.a.a(MessageProvider.p);
                    v0_1 = ContentUris.withAppendedId(com.umeng.message.provider.a.f, v2);
                    this.getContext().getContentResolver().notifyChange(v0_1, null);
                    return v0_1;
                }
                case 6: {
                    v2 = this.f.insertWithOnConflict("MsgLogIdTypeStore", null, arg10, 5);
                    if (v2 <= v6) {
                        return v1;
                    }
                    com.umeng.message.provider.a.a(MessageProvider.p);
                    return ContentUris.withAppendedId(com.umeng.message.provider.a.g, v2);
                }
                case 7: {
                    v2 = this.f.insertWithOnConflict("MsgLogStoreForAgoo", null, arg10, 5);
                    if (v2 <= v6) {
                        return v1;
                    }
                    com.umeng.message.provider.a.a(MessageProvider.p);
                    return ContentUris.withAppendedId(com.umeng.message.provider.a.h, v2);
                }
                case 8: {
                    v2 = this.f.insertWithOnConflict("MsgLogIdTypeStoreForAgoo", null, arg10, 5);
                    if (v2 <= v6) {
                        return v1;
                    }
                    com.umeng.message.provider.a.a(MessageProvider.p);
                    return ContentUris.withAppendedId(com.umeng.message.provider.a.i, v2);
                }
                case 9: {
                    v2 = this.f.insertWithOnConflict("MsgConfigInfo", null, arg10, 5);
                    if (v2 <= v6) {
                        return v1;
                    }
                    com.umeng.message.provider.a.a(MessageProvider.p);
                    return ContentUris.withAppendedId(com.umeng.message.provider.a.j, v2);
                }
            }
            return v1;
        } catch (Throwable v0) {
            v0.printStackTrace();
            return v1;
        }
    }

    public boolean onCreate() {
        try {
            MessageProvider.p = this.getContext();
            this.b();
            UriMatcher v0_1 = MessageProvider.b;
            com.umeng.message.provider.a.a(MessageProvider.p);
            v0_1.addURI(com.umeng.message.provider.a.a, "MessageStores", 1);
            v0_1 = MessageProvider.b;
            com.umeng.message.provider.a.a(MessageProvider.p);
            v0_1.addURI(com.umeng.message.provider.a.a, "MsgTemps", 2);
            v0_1 = MessageProvider.b;
            com.umeng.message.provider.a.a(MessageProvider.p);
            v0_1.addURI(com.umeng.message.provider.a.a, "MsgAlias", 3);
            v0_1 = MessageProvider.b;
            com.umeng.message.provider.a.a(MessageProvider.p);
            v0_1.addURI(com.umeng.message.provider.a.a, "MsgAliasDeleteAll", 4);
            v0_1 = MessageProvider.b;
            com.umeng.message.provider.a.a(MessageProvider.p);
            v0_1.addURI(com.umeng.message.provider.a.a, "MsgLogStores", 5);
            v0_1 = MessageProvider.b;
            com.umeng.message.provider.a.a(MessageProvider.p);
            v0_1.addURI(com.umeng.message.provider.a.a, "MsgLogIdTypeStores", 6);
            v0_1 = MessageProvider.b;
            com.umeng.message.provider.a.a(MessageProvider.p);
            v0_1.addURI(com.umeng.message.provider.a.a, "MsgLogStoreForAgoos", 7);
            v0_1 = MessageProvider.b;
            com.umeng.message.provider.a.a(MessageProvider.p);
            v0_1.addURI(com.umeng.message.provider.a.a, "MsgLogIdTypeStoreForAgoos", 8);
            v0_1 = MessageProvider.b;
            com.umeng.message.provider.a.a(MessageProvider.p);
            v0_1.addURI(com.umeng.message.provider.a.a, "MsgConfigInfos", 9);
        } catch (Throwable v0) {
            v0.printStackTrace();
        }

        try {
            this.getContext().startService(new Intent(this.getContext(), Class.forName("com.ss.android.newmedia.message.MessageHandler")));
        } catch (Throwable v0) {
        }

        return true;
    }

    public Cursor query(Uri arg10, String[] arg11, String arg12, String[] arg13, String arg14) {
        Cursor v0_1;
        Throwable v1;
        Cursor v8 = null;
        try {
            switch (MessageProvider.b.match(arg10)) {
                case 2:
                    v0_1 = this.d.query("MsgTemp", arg11, arg12, arg13, null, null, arg14);
                    break;
                case 3:
                    v0_1 = this.d.query("MsgAlias", arg11, arg12, arg13, null, null, arg14);
                    break;
                case 5:
                    v0_1 = this.f.query("MsgLogStore", arg11, arg12, arg13, null, null, arg14);
                    break;
                case 7:
                    v0_1 = this.f.query("MsgLogStoreForAgoo", arg11, arg12, arg13, null, null, arg14);
                    break;
                case 8:
                    v0_1 = this.f.query("MsgLogIdTypeStoreForAgoo", arg11, arg12, arg13, null, null, arg14);
                    break;
                case 9:
                    v0_1 = this.f.query("MsgConfigInfo", arg11, arg12, arg13, null, null, arg14);
                    break;
                default:
                    v0_1 = v8;
                    break;
            }
        } catch (Throwable v0) {
            v1 = v0;
            v0_1 = v8;
            v1.printStackTrace();
            return v0_1;
        }

        if (v0_1 != null) {
            v0_1.setNotificationUri(this.getContext().getContentResolver(), arg10);
            return v0_1;
        }

        return v0_1;
    }

    public int update(Uri arg8, ContentValues arg9, String arg10, String[] arg11) {
        Throwable v1;
        int v0_1;
        try {
            switch (MessageProvider.b.match(arg8)) {
                case 1: {
                    v0_1 = this.d.updateWithOnConflict("MessageStore", arg9, arg10, arg11, 5);
                    break;
                }
                case 2: {
                    v0_1 = this.d.updateWithOnConflict("MsgTemp", arg9, arg10, arg11, 5);
                    break;
                }
                case 3: {
                    this.d.updateWithOnConflict("MsgAlias", arg9, arg10, arg11, 5);
                    v0_1 = 0;
                    break;
                }
                case 9: {
                    v0_1 = this.f.updateWithOnConflict("MsgConfigInfo", arg9, arg10, arg11, 5);
                    break;
                }
                default:
                    v0_1 = 0;
                    break;
            }

        } catch (Throwable v0) {
            v1 = v0;
            v0_1 = 0;
            v1.printStackTrace();
            return v0_1;
        }
        try {
            this.getContext().getContentResolver().notifyChange(arg8, null);
            return v0_1;
        } catch (Throwable v01) {
            v01.printStackTrace();
            return v0_1;
        }
    }
}

